package insight.google.game.era.data;

import insight.google.game.era.bo.GameActionLog;

import insight.google.game.era.bo.GameRoom;
import insight.google.game.era.bo.GameUser;
import insight.google.game.era.bo.Message;
import insight.google.game.era.bo.card.CharacterCard;
import insight.google.game.era.bo.card.GameCard;
import insight.google.game.era.bo.card.RoleCard;
import insight.google.game.era.core.ChatCenter;
import insight.google.game.era.mgr.FlowManager;
import insight.google.game.era.mgr.GameLogManager;

import java.util.HashMap;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.google.appengine.api.datastore.Key;


public class GameCenterDataManager extends AppDataManager {

	private static final Log log = LogFactory.getLog(GameCenterDataManager.class);
	
	public GameCenterDataManager(){
		this.isDetachable = true;
		super.initialize();
	}
	
	public GameCenterDataManager(TransactionHandler txHandler){
		this.txHandler = txHandler;
		super.initialize();
	}
	
	
	public GameCenterDataManager(TransactionHandler txHandler,boolean isDetachable){
		this.txHandler = txHandler;
		this.isDetachable = isDetachable;
		super.initialize();
	}
	
//	@Override
//	public boolean addToDataStore(Object o) {
//		try {
//			this.dataStoreMgr.save(o,pManager);
//		} catch (Exception e) {
//			// TODO Auto-generated catch block
//			e.printStackTrace();
//		}
//		return false;
//	}

	@Override
	public List<GameCenter> findAll() {
		// TODO Auto-generated method stub
		try {
			return dataStoreMgr.get(GameCenter.class,pManager,this.isDetachable);
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}
	
	public GameCenter find() {
		List<GameCenter> gameCenter=this.findAll();
		if(gameCenter != null && gameCenter.size()>0 ){
			return gameCenter.get(0);
		}
		return null;
	}

	public ChatCenter findChatCenterById(Key chatCenterId){
		return (ChatCenter)this.dataStoreMgr.getById(ChatCenter.class, chatCenterId, this.pManager,this.isDetachable);
	}
	
	public ChatCenter findChatCenter(int owner){
		StringBuffer cond = new StringBuffer();
		cond.append("chatCenterOwner == " + owner + "");
		List<ChatCenter> list= null;
		try {
			list = (List<ChatCenter>)DataStoreManager.getInstance().getByCondition(ChatCenter.class, cond.toString(),pManager,this.isDetachable);
			if(list != null){
				for (ChatCenter chatCenter : list) {
					return chatCenter;
				}
			}
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}
	
	
	public void clearDataStore(){
		try {
			List dataList = dataStoreMgr.get(GameCenter.class, this.pManager,this.isDetachable);
			
			this.delete(dataList,"GameCenter");
			
			dataList = dataStoreMgr.get(User.class, this.pManager,this.isDetachable);
			this.delete(dataList,"User");
			
			dataList = dataStoreMgr.get(UserHistory.class, this.pManager,this.isDetachable);
			this.delete(dataList,"UserHistory");
			
			dataList = dataStoreMgr.get(Room.class, this.pManager,this.isDetachable);
			this.delete(dataList,"Room");
			
//			dataList = dataStoreMgr.get(ChatCenter.class, this.pManager,this.isDetachable);
//			this.delete(dataList,"ChatCenter");
			
			
			
//			dataList = dataStoreMgr.get(GameUser.class, this.pManager,this.isDetachable);
//			this.delete(dataList,"GameUser");
//			
//			dataList = dataStoreMgr.get(GameCard.class, this.pManager,this.isDetachable);
//			this.delete(dataList,"GameCard");
			

//			dataList = dataStoreMgr.get(RoleCard.class, this.pManager,this.isDetachable);
//			this.delete(dataList,"RoleCard");


//			dataList = dataStoreMgr.get(CharacterCard.class, this.pManager,this.isDetachable);
//			this.delete(dataList,"CharacterCard");
			

//			dataList = dataStoreMgr.get(FlowManager.class, this.pManager,this.isDetachable);
//			this.delete(dataList,"FlowManager");
			
//			dataList = dataStoreMgr.get(GameActionLog.class, this.pManager,this.isDetachable);
//			this.delete(dataList,"GameActionLog");
			
			
			
//			dataList = dataStoreMgr.get(Message.class, this.pManager,this.isDetachable);
//			this.delete(dataList,"Message");
			
			
			dataList = dataStoreMgr.get(GameHistory.class, this.pManager,this.isDetachable);
			this.delete(dataList,"GameHistory");
			
			dataList = dataStoreMgr.get(GameHistoryDetail.class, this.pManager,this.isDetachable);
			this.delete(dataList,"GameHistoryDetail");
			
//			dataList = dataStoreMgr.get(GameLogManager.class, this.pManager,this.isDetachable);
//			this.delete(dataList,"GameLogManager");
			dataList = dataStoreMgr.get(GameRoom.class, this.pManager,this.isDetachable);
			this.delete(dataList,"GameRoom");
			
			this.commit();
		} catch (InterruptedException e) {
			e.printStackTrace();
		}finally{
			this.close();
		}
		
	}
	
	private void delete(List dataList, String className){
		if(dataList != null && dataList.size()>0){
			log.debug("Deleteing ...   "+ className+"  ......no. of record ###  " + dataList.size());
			dataStoreMgr.deleteAll(dataList, this.pManager);
		}
	}
}
